package com.drug.mingkang.service.impl;

import com.drug.mingkang.model.HqImm;
import com.drug.mingkang.model.HqRicher;
import com.drug.mingkang.mapper.HqRicherMapper;
import com.drug.mingkang.model.vo.FinancialDetailsVo;
import com.drug.mingkang.permission.model.PerUsers;
import com.drug.mingkang.service.HqRicherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 进账明细(HqRicher)表服务实现类
 *
 * @author wenchangjian
 * @since 2020-08-11 12:00:13
 */
@Service("hqRicherService")
public class HqRicherServiceImpl implements HqRicherService {
    @Resource
    private HqRicherMapper hqRicherMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param richerId 主键
     * @return 实例对象
     */
    @Override
    public HqRicher queryById(String richerId) {
        return this.hqRicherMapper.queryById(richerId);
    }

    @Override
    public String selectCompanyId(String companyId) {
        return this.hqRicherMapper.selectCompanyId(companyId);
    }

    @Override
    public List<Double> findTodayIncomeOrMonthIncome(String i) {
        List<Double> list1=new ArrayList<>();
        List<Double> list;
        if ("0".equals(i)){
            list=this.hqRicherMapper.findTodayIncomeOrMonthIncomes();
        }else {
            list=this.hqRicherMapper.findTodayIncomeOrMonthIncome(i);
        }

        //判断 如果等于null就替换为0
        for (int j = 0; j < list.size(); j++) {
            if (list.get(j)==null){
                list.set(j, (double) 0);
            }
        }
        //月收入 四舍五入 保留俩位
        //%.2f 中的% 表示 小数点前任意位数   2 表示两位小数 格式后的结果为f 表示浮点型
        list1.add(Double.valueOf(String.format("%.2f", list.get(0)-list.get(3))));
        //日收入
        list1.add(Double.valueOf(String.format("%.2f", list.get(1)-list.get(2))));
        return list1;
    }

    @Override
    public Double findSumMoney(String i) {
        if ("0".equals(i)){
            return this.hqRicherMapper.findSumMoneys();
        }else{
            return this.hqRicherMapper.findSumMoney(i);
        }
    }

    @Override
    public List<FinancialDetailsVo> findCompanyFinancialDetails(FinancialDetailsVo vo, HttpSession session) {
        PerUsers perUsers = (PerUsers)session.getAttribute("perUsers");
        //判断是否总公司
        String z="0";
        if (z.equals(perUsers.getCompanyId())){
            return hqRicherMapper.findCompanyFinancialDetailsA(vo);
        }else{
            return hqRicherMapper.findCompanyFinancialDetails(perUsers.getCompanyId(),vo);
        }
    }

    @Override
    public int findCompanyFinancialDetailsCount(FinancialDetailsVo vo, HttpSession session) {
        PerUsers perUsers = (PerUsers)session.getAttribute("perUsers");
        //判断是否总公司
        String z="0";
        if (z.equals(perUsers.getCompanyId())){
            return hqRicherMapper.findCompanyFinancialDetailsCount(z,vo);
        }else{
            return hqRicherMapper.findCompanyFinancialDetailsCounts(perUsers.getCompanyId(),vo);
        }
    }

    @Override
    public boolean insertPurchase(HqRicher hqRicher) {
        return this.hqRicherMapper.insertPurchase(hqRicher);
    }

    @Override
    public int selectCounts(FinancialDetailsVo vo) {
        return this.hqRicherMapper.selectCounts(vo);
    }

    @Override
    public int selectCount(FinancialDetailsVo vo) {
        return this.hqRicherMapper.selectCount(vo);
    }

    /**
     * 查询多条数据
     *
     * @param vo
     * @return 对象列表
     */
    @Override
    public List<HqRicher> queryAllByLimit(FinancialDetailsVo vo) {
        return this.hqRicherMapper.queryAllByLimit(vo);
    }

    @Override
    public List<HqRicher> queryAllByLimits(FinancialDetailsVo vo) {
        return this.hqRicherMapper.queryAllByLimits(vo);
    }

    @Override
    public List<HqImm> showGs() {
        return this.hqRicherMapper.showGs();
    }

    /**
     * 新增数据
     *
     * @param hqRicher 实例对象
     * @return 实例对象
     */
    @Override
    public HqRicher insert(HqRicher hqRicher) {
        this.hqRicherMapper.insert(hqRicher);
        return hqRicher;
    }

    /**
     * 修改数据
     *
     * @param hqRicher 实例对象
     * @return 实例对象
     */
    @Override
    public HqRicher update(HqRicher hqRicher) {
        this.hqRicherMapper.update(hqRicher);
        return this.queryById(hqRicher.getRicherId());
    }

    /**
     * 通过主键删除数据
     *
     * @param richerId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String richerId) {
        return this.hqRicherMapper.deleteById(richerId) > 0;
    }
}